package windows 
{
	import com.bit101.components.Window;
	import flash.display.Sprite;
	import flash.events.MouseEvent
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author artem.kolesnikov 
	 */
	public class DraggableWindow extends Window 
	{
		private var nub:Sprite;
		public function DraggableWindow() 
		{
			nub = new Sprite();
			nub.graphics.lineStyle(1);
			nub.graphics.beginFill(0x000000);
			nub.graphics.drawRect(0, 0, -10, -10);
			nub.graphics.endFill();
			nub.buttonMode = true;
			addChild(nub);
			
			//nub.x = width - nub.width + 10 + 2;
			//nub.y = height - nub.height - 0 + 2;
		
			onStageMouseMoveFromNub(null);
			//addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
			addEventListener(MouseEvent.MOUSE_DOWN, onNubMouseDown, false, 0, true);
		}
		
		override public function set width(value:Number):void {
			super.width = value;
			//updateNunbPosition();
		}
		
		override public function set height(value:Number):void {
			super.height = value;
			//updateNunbPosition();
		}
		

		
		
		//called by the MouseEvent.MOUSE_DOWN event on the nub
		private function onNubMouseDown(event:MouseEvent):void
		{
			if (((event.localX > width - 20) && (event.localX < width) &&
			(event.localY > height - 50) && (event.localY < height))) {
				trace(12323);
			}
			//if(event.localX (width-10))
			//stop the event from propagating (so this, the SnapshotRectangle instance, does not hear it)
			event.stopImmediatePropagation();
			
			//start dragging the nub, without snapping mouse to center, constraining to _rect
			nub.startDrag(false, new Rectangle(0,0,10000,1000));
			//add _onStageMouseUpFromNub as a callback to the mouseUp event on the stage
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUpFromNub, false, 0, true);
			//add _onStageMouseMoveFromNub as a callback to the mouseMove event on the stage
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMoveFromNub, false, 0, true);
		}
		
		//called by the MouseEvent.MOUSE_UP event on the stage, after _onMouseDown has been run
		//to stop the drag move operation
		private function onStageMouseUp(event:MouseEvent):void
		{
			//stop dragging (this, the SnapshotRectangle instance)
			stopDrag();
			//remove _onStageMouseUp as a callback from the mouseUp event on the stage
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMoveFromNub);
		}
		
		//called by the MouseEvent.MOUSE_UP event on the stage, after _onNubMouseDown has been run
		//to stop the drag resize operation
		private function onStageMouseUpFromNub(event:MouseEvent):void
		{
			//stop dragging (nub)
			stopDrag();
			//remove _onStageMouseUpFromNub as a callback from the mouseUp event on the stage
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUpFromNub);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMoveFromNub);
		}
		
		//called by the MouseEvent.MOUSE_MOVE event on the stage, after _onNubMouseDown has been run
		//to update the drag resizing
		private function onStageMouseMoveFromNub(event:MouseEvent):void
		{
			//force nub's x and y to integers
			nub.x = Math.round(nub.x);
			nub.y = Math.round(nub.y);
			
			this.width = nub.x;
			this.height = nub.y;
			
			//reflect the accurate dimensions in the tf_dims textfield
			//so user knows how big an image he'll create by snapping
			//this.x = Math.round(this.x);
			//this.y = Math.round(this.y);
		
			//nameTF.x = width / 2 - nameTF.width/2;
			//nameTF.y = height / 2 - nameTF.height / 2;
			//nameTF.visible = ((nub.x > nameTF.width ))
		}
		
	}

}